#include <cstdio>
#include <cstdlib>
#include <time.h>
#include "includeall.h"

using namespace Oribi;

OribiApp::OribiApp()
{
    srand(time(0));
}

int OribiApp::initialise()
{
    int stat;
    stat = conf.initialise();
    if (stat)
        return stat;

    stat = grc.initialise(conf);
    if (stat)
        return stat;

    stat = res.initialise(conf);
    if (stat)
        return stat;

    return 0;
}

void OribiApp::shutDown()
{
    grc.shutDown();
}

void OribiApp::mainLoop()
{
    Game game(1);
    game.createMap(20);
    game.initialise(conf, res);
    Player *p1 = game.getPlayer(1);
    if (!p1)
        return;
    EntityManager& p1em = p1->getEntityManager();
    Human ent;
    ent.initSprite(res, conf);
    p1em.addEntity(ent);

    Antelope a;
    a.initSprite(res, conf);
    a.setPosition({0, 1});
    p1em.addEntity(a);

    int state = RUNNING_BIT;

    while (state & RUNNING_BIT)
    {
        inp.handleInput(*this, &state);
        game.update();

        grc.beginFrame();
        game.drawEverything(grc);
        grc.blitSprites();
        grc.endFrame();
    }
}

GraphicContext& OribiApp::getGraphicContext()
{
    return grc;
}

Resources& OribiApp::getResources()
{
    return res;
}
